//
//  LWNavVC.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/19.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit

class LWNavVC: UINavigationController ,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
//        print(viewControllers.count)
        
        if viewControllers.count > 0 {
        if viewControllers.count == 1 {
            //获取上一级的title
            let title = viewControllers.first?.title
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
        }else{
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
        }
        
        viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"

            //隐藏底部的tabBar
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    func backAction() -> () {
        
        popViewController(animated: true)
        
    }
    //解决手势问题:解决设置返回按钮之后无法进行手势拖动
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1{
            return false
        }
        
        return true
    }

}
